package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.TravelInItemModels;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 16/9/2.
 */
public class TravelInItemAdapter extends RecyclerView.Adapter<TravelInItemAdapter.InItemViewHolder> {
    private Context context;
    private TravelInItemModels travelInItemModels;
    private int count;
    private TravelInDayAdapter adapter;

    public TravelInItemAdapter(Context context, TravelInItemModels travelInItemModels) {
        this.context = context;
        this.travelInItemModels = travelInItemModels;
        this.count = travelInItemModels.getTrip_days().size() + 1;
    }

    @Override
    public InItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_travel_in_item, null);
        InItemViewHolder holder = new InItemViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(InItemViewHolder holder, int position) {
        if (position == 0) {
            holder.line.setVisibility(View.VISIBLE);
            holder.name.setText(travelInItemModels.getName());
            holder.startDate.setText(travelInItemModels.getStart_date());
            holder.travelDay.setText(travelInItemModels.getTrip_days().size() + "天");
            holder.travelWhichimage.setText(travelInItemModels.getPhotos_count() + "图");
            holder.day.setVisibility(View.GONE);
            Picasso.with(context).load(travelInItemModels.getFront_cover_photo_url()).into(holder.frontCoverPhotoUrl);
            Picasso.with(context).load(travelInItemModels.getUser().getImage()).into(holder.image);
        } else {
            int i = position - 1;
            holder.name.setVisibility(View.GONE);
            holder.startDate.setVisibility(View.GONE);
            holder.travelDay.setVisibility(View.GONE);
            holder.travelWhichimage.setVisibility(View.GONE);
            holder.frontCoverPhotoUrl.setVisibility(View.GONE);
            holder.image.setVisibility(View.GONE);


            holder.day.setText("DAY" + travelInItemModels.getTrip_days().get(i).getDay());
            RecyclerView recyclerView = new RecyclerView(context);
            adapter = new TravelInDayAdapter(context, travelInItemModels.getTrip_days().get(i), travelInItemModels.getNotes_likes_comments());
            recyclerView.setAdapter(adapter);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            recyclerView.setLayoutManager(linearLayoutManager);
            holder.linearLayout.addView(recyclerView);
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class InItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView frontCoverPhotoUrl;
        private ImageView image;
        private TextView name;
        private TextView startDate;
        private View line;
        private TextView travelDay;
        private TextView travelWhichimage;
        private TextView day;
        private LinearLayout linearLayout;

        public InItemViewHolder(View itemView) {
            super(itemView);
            frontCoverPhotoUrl = (ImageView) itemView.findViewById(R.id.front_cover_photo_url);
            image = (ImageView) itemView.findViewById(R.id.image);
            name = (TextView) itemView.findViewById(R.id.name);
            startDate = (TextView) itemView.findViewById(R.id.start_date);
            line = (View) itemView.findViewById(R.id.line);
            travelDay = (TextView) itemView.findViewById(R.id.travel_day);
            travelWhichimage = (TextView) itemView.findViewById(R.id.travel_whichimage);
            day = (TextView) itemView.findViewById(R.id.day);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.lin_recyclerview);
        }
    }
}
